package com.evaluationsystem.config.securityConfig;

import com.evaluationsystem.config.securityFilter.LoginSuccessHandlerFilter;
import com.evaluationsystem.config.securityFilter.LoginFailureHandlerFilter;
import com.evaluationsystem.system.serivce.UserInfoService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    UserInfoService service;

    @Resource
    LoginFailureHandlerFilter failureHandlerFilter;

    @Resource
    LoginSuccessHandlerFilter successHandlerFilter;

    @Bean
    protected BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(service).passwordEncoder(bCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //释放静态支援
                .antMatchers("/public/**","/sys/**","/erro/**","/sys/*/**", "/excelTemplate/**").permitAll()
                //其他 接口都需要登录后可以访问
                .anyRequest()
                .authenticated()
                .and()
                //配置 自定义 登录 页面
                .formLogin().loginPage("/login")
                .loginProcessingUrl("/login")
                .successHandler(successHandlerFilter)
                .failureHandler(failureHandlerFilter)
                .permitAll()
                .and()
                .logout().logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .invalidateHttpSession(true)
                .permitAll()
                .and()
                .csrf().disable()
                /*解决 iframe 不加载 的问题 */
                .headers().frameOptions().disable()
                .and()
                .sessionManagement()
                /*设置 session 制创建 机*/
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                /*会话超时 返回 URL*/
                .invalidSessionUrl("/login")
                .sessionFixation();
    }


    /*@Override
    public void configure(WebSecurity web) throws Exception {
        //配置静态文件不需要认证
        web.ignoring().antMatchers("/static/**");
    }*/








































}
